
/**		
 * 
 *	uk.co.lmfm.guppy.view.UpdateMediator
 *	
 *	@version 1.00 | Jan 12, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view 
{
	import uk.co.lmfm.guppy.application.enum.ApplicationConstants;
	import uk.co.lmfm.guppy.model.vo.ViewVO;
	import uk.co.lmfm.guppy.view.components.events.UpdateViewEvent;
	import uk.co.lmfm.guppy.view.components.update.UpdateView;
	import uk.co.lmfm.net.subversion.vo.SVNLogReportVO;

	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	/**
	 * UpdateMediator
	 */
	public class UpdateMediator extends Mediator implements IMediator 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		public static const NAME : String = "UpdateMediator";

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function UpdateMediator(viewComponent : UpdateView)
		{
			super(NAME, viewComponent);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		override public function onRegister() : void
		{
			super.onRegister();
			
			component.addEventListener(UpdateViewEvent.FORCE_UPDATE, onForceUpdate);			//component.addEventListener(UpdateViewEvent.APPLY_SETTINGS, onApplySettings);
		}

		override public function listNotificationInterests() : Array
		{
			return [
					ApplicationConstants.CHANGE_VIEW,
					ApplicationConstants.POLL_VIEWS_FOR_DEFINITIONS,
					ApplicationConstants.CHECKOUT_ITEM_STARTED,					ApplicationConstants.CHECKOUT_ITEM_COMPLETE,
					ApplicationConstants.CHECKOUT_ITEM_PROGRESS,					ApplicationConstants.CHECKOUT_PROGRESS,					ApplicationConstants.CHECKOUT_COMPLETE,					ApplicationConstants.CHECKOUT_ACTIVITY,					ApplicationConstants.CHECKOUT_MESSAGE
					];
		}

		override public function handleNotification(notification : INotification) : void
		{
			super.handleNotification(notification);
			
			var name : String = notification.getName();
			var body : Object = notification.getBody();
			
			switch(name)
			{
				case ApplicationConstants.CHANGE_VIEW : 
				
					if(body == NAME)
					{
						component.show();
					}
					else
					{
						component.hide();
					}
				
					break;
					
				case ApplicationConstants.POLL_VIEWS_FOR_DEFINITIONS :
				
					var definition : ViewVO = new ViewVO();
			
					definition.name = UpdateMediator.NAME;
					definition.label = "Update";
			
					sendNotification(ApplicationConstants.GOT_VIEW_DEFINITION, definition);
			
					break;
					
				case ApplicationConstants.CHECKOUT_MESSAGE :
				
					component.setMessage(String(body));
				
					break;
					
				case ApplicationConstants.CHECKOUT_ACTIVITY :
				
					component.addActivity(String(body));
				
					break;
					
				case ApplicationConstants.CHECKOUT_ITEM_STARTED :
									component.setMessage("Downloading: " + body);
				
					break;
					
				case ApplicationConstants.CHECKOUT_ITEM_PROGRESS :
				
					component.setItemProgress(Number(body));
				
					break;
					
				case ApplicationConstants.CHECKOUT_ITEM_COMPLETE :
				
					component.setMessage("Complete...");
					component.addActivity(String(body));
				
					break;
				
				case ApplicationConstants.CHECKOUT_PROGRESS :
				
					component.setTotalProgress(Number(body));
				
					break;
					
				case ApplicationConstants.CHECKOUT_COMPLETE :
				
					var report : SVNLogReportVO = SVNLogReportVO(body);
					
					component.setTotalProgress(1.0);
					component.setMessage("Update complete!\n" + report.paths.length + " items updated. At revision: " + report.latestRevisionNumber);
				
					break;
			}
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onForceUpdate(event : UpdateViewEvent) : void
		{
			component.clear();
			component.setMessage("Forcing update...");
			sendNotification(ApplicationConstants.FORCE_REPOSITORY_CHECKOUT);
		}

		/*private function onApplySettings(event : UpdateViewEvent) : void
		{
			sendNotification(ApplicationConstants.APPLY_FDT_SETTINGS);
		}*/

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get component() : UpdateView
		{
			return viewComponent as UpdateView;
		}
	}
}
